package com.sgb.kjwl.viewmodel.self.logistics;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.OfferDetailsEntity;
import com.sgb.kjwl.view.ui.activity.self.logistics.OfferOwnerDetailsActivity;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/4/8
 */
public class OfferOwnerDetailsModel extends AndroidViewModel {
    public MutableLiveData<OfferDetailsEntity> detailsLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> refreshLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldAgree = new MutableLiveData<>();

    public OfferOwnerDetailsModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 请求最新的车主报价信息
     */
    public void getOfferOwnerDetails(OfferOwnerDetailsActivity activity, String goodsOfferNo) {
        if (TextUtils.isEmpty(goodsOfferNo))
            return;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getOfferOwnerDetails(goodsOfferNo).enqueue(new ICallBack<BaseEntity<OfferDetailsEntity>>() {
            @Override
            public void onSuccess(BaseEntity<OfferDetailsEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getState().equals(APIManager.RESPONSE_OK)) {
                    detailsLiveData.postValue(data.getData());
                } else {
                    ToastUtil.show(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 同意车主报价
     */
    public void agreeOffer(OfferOwnerDetailsActivity activity, String id, String no) {
        Map map = new HashMap();
        map.put("no", id);
        map.put("goodsSourceNo", no);
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().shipperAgreeOffer(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getCode() == null) {
                    return;
                }
                //报价成功
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    refreshLiveData.postValue(true);
                }
                //车辆状态异常
                else if (response.body().getState().equals(APIManager.RESPONSE_FAIL)
                        && response.body().getCode().equals(APIManager.CAR_ABNORMAL)) {
                    if (!TextUtils.isEmpty(response.body().getMsg())) {
                        new RoundDialog.Builder(activity)
                                .title("车辆状态异常")
                                .content(response.body().getMsg())
                                .confirm("确定")
                                .singleBtn(true)
                                .contentGravity(Gravity.CENTER)
                                .cancelable(false)
                                .show();
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 同意车主报价 普通货源
     *
     * @param id goodsOffer报价编号
     * @param no 货源编号
     */
    public void agreeOfferGeneral(Context context, String id, String no) {
        Map map = new HashMap();
        map.put("no", id);
        map.put("goodsSourceNo", no);
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().agreeGeneralOffer(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getCode() == null) {
                    return;
                }
                //报价成功
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldAgree.postValue(true);
                }
                //车辆状态异常
                else if (response.body().getState().equals(APIManager.RESPONSE_FAIL)
                        && response.body().getCode().equals(APIManager.CAR_ABNORMAL)) {
                    if (!TextUtils.isEmpty(response.body().getMsg())) {
                        new RoundDialog.Builder(context)
                                .title("车辆状态异常")
                                .content(response.body().getMsg())
                                .confirm("确定")
                                .singleBtn(true)
                                .contentGravity(Gravity.CENTER)
                                .cancelable(false)
                                .show();
                    }
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
